package top.wshape1.shiziapi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.wshape1.shiziapi.project.domain.Folder;
import top.wshape1.shiziapi.project.domain.vo.CreateDocVo;
import top.wshape1.shiziapi.project.mapper.FolderMapper;
import top.wshape1.shiziapi.project.service.ApiService;
import top.wshape1.shiziapi.project.service.FolderService;
import top.wshape1.shiziapi.project.service.MarkdownService;
import top.wshape1.shiziapi.project.service.RtfService;

import java.util.Date;
import java.util.List;

/**
 * @author Wshape1
 * @description 针对表【t_project_folder】的数据库操作Service实现
 * @since 2023-10-22 18:21:50
 */
@Service
public class FolderServiceImpl extends ServiceImpl<FolderMapper, Folder>
        implements FolderService {

    @Resource
    private ApiService apiService;

    @Resource
    private MarkdownService markdownService;

    @Resource
    private RtfService rtfService;

    @Override
    public Long create(CreateDocVo createDocVo) {

        Folder folder = new Folder();

        folder.setProjectId(createDocVo.getProjectId());
        folder.setName(createDocVo.getName());
        folder.setParentFolderId(createDocVo.getParentFolderId());
        folder.setOrder(createDocVo.getOrder());
        folder.setCreateTime(new Date());

        this.save(folder);

        return folder.getId();

    }

    @Override
    public void rename(Long id, String name) {

        this.update(new LambdaUpdateWrapper<Folder>()
                .eq(Folder::getId, id)
                .set(Folder::getName, name));

    }

    @Transactional
    @Override
    public void delete(Long id) {

        // 删除目录下所有文档
        deleteFolderRec(id);

    }

    @Override
    public List<Long> getSubFolderIds(Long parentFolderId) {
        return this.getBaseMapper().selectSubFolderIds(parentFolderId);
    }

    @Override
    public void updateOrderAndParentFolderId(Long id, Integer order, Long parentFolderId) {
        this.update(new LambdaUpdateWrapper<Folder>()
                .eq(Folder::getId, id)
                .set(Folder::getOrder, order)
                .set(Folder::getParentFolderId, parentFolderId)
        );
    }

    @Override
    public void updateOrder(Long id, Integer order) {
        this.update(new LambdaUpdateWrapper<Folder>()
                .eq(Folder::getId, id)
                .set(Folder::getOrder, order)
        );
    }

    @Transactional
    @Override
    public void clearProject(long projectId) {
        this.remove(new LambdaQueryWrapper<Folder>()
                .eq(Folder::getProjectId, projectId));
    }

    /**
     * 递归删除目录及目录内的文档
     *
     * @param id 目录id
     */
    private void deleteFolderRec(Long id) {
        // 先删除parentFolderId为{id}的文档
        apiService.deleteByParentFolderId(id);
        markdownService.deleteByParentFolderId(id);
        rtfService.deleteByParentFolderId(id);


        // 再删除parentFolderId为{id}的目录
        List<Long> list = getSubFolderIds(id);
        if (list != null && !list.isEmpty()) {
            list.forEach(this::deleteFolderRec);
        }

        // 最后删除id为{id}的目录 （也就是自己）
        this.removeById(id);

    }

}




